using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;

public class GameManager : MonoSingleton<GameManager>
{
	private UI_InGame _mUI_InGame;

	public static bool _无限气力;

	public static bool _无限酒;

	public static bool _无限豆子;

	public static bool _10倍攻击;

	public static bool _无敌;

	public static bool _暂停游戏;

	public bool _mCanPlay;

	private static bool _读取三连;

	private static float _m已三连;

	private PlayerController _PC;

	public static bool _Can上香 = true;

	private Vector2 _Pos_直面天命;

	public static bool _Can传送 = true;

	public static bool _传送动画结束 = false;

	public static bool _开启时间积累 = false;

	private Dictionary<string, bool> _mDict_EnemyID_CanInit;

	public float _GameTime;

	public UI_InGame _UI_InGame
	{
		get
		{
			if (_mUI_InGame == null)
			{
				_mUI_InGame = Object.Instantiate(Resources.Load<UI_InGame>("UI/UI_InGame"), base.transform);
			}
			return _mUI_InGame;
		}
	}

	public static bool _CanPlay
	{
		get
		{
			return MonoSingleton<GameManager>.Instance._mCanPlay;
		}
		set
		{
			MonoSingleton<GameManager>.Instance._mCanPlay = value;
		}
	}

	public static float _已三连
	{
		get
		{
			if (!_读取三连)
			{
				_读取三连 = true;
				_m已三连 = PlayerPrefs.GetFloat("已三连", 1f);
			}
			return _m已三连;
		}
		set
		{
			_m已三连 = value;
			PlayerPrefs.SetFloat("已三连", _m已三连);
			PlayerPrefs.Save();
		}
	}

	public PlayerController _Pub_PC => _PC;

	public Dictionary<string, bool> _Dict_EnemyID_CanInit
	{
		get
		{
			if (_mDict_EnemyID_CanInit == null)
			{
				_mDict_EnemyID_CanInit = new Dictionary<string, bool>();
			}
			return _mDict_EnemyID_CanInit;
		}
	}

	public ItemUI_EnemyHP F_Get_ItemUI_EnemyHP()
	{
		return _UI_InGame.F_Get_ItemUI_EnemyHP();
	}

	public ItemUI_BossHP F_Get_ItemUI_BossHP()
	{
		return _UI_InGame.F_Get_ItemUI_BossHP();
	}

	public ItemUI_存档点 F_Get_ItemUI_存档点()
	{
		return _UI_InGame.F_Get_ItemUI_存档点();
	}

	public ItemUI_打坐点 F_Get_ItemUI_打坐点()
	{
		return _UI_InGame.F_Get_ItemUI_打坐点();
	}

	private void Start()
	{
		//Debug.unityLogger.logEnabled = false;
		_GameTime = PlayerPrefs.GetFloat("游戏时长", 0f);
		if (_PC == null)
		{
			MonoSingleton<InputContrl>.Instance.Init();
			_PC = Object.Instantiate(Resources.Load<PlayerController>("Player"), base.transform);
			_UI_InGame.F_注册玩家时绑定事件(_PC);
			_UI_InGame.F_UI_定身术CD(0.1f);
			if (MonoSingleton<MySceneManager>.Instance._CurrSceneID == 0)
			{
				Debug.LogError("初始隐藏玩家");
				_PC.gameObject.SetActive(value: false);
				_CanPlay = false;
				_UI_InGame.F_隐藏();
				_UI_InGame._CG_遮罩.alpha = 1f;
				_UI_InGame._CG_遮罩.blocksRaycasts = true;
			}
			else
			{
				Debug.LogError("初始显示玩家");
				_PC.transform.position = MonoSingleton<MySceneManager>.Instance.F_Get_MapPonit_传送();
				_PC.gameObject.SetActive(value: true);
				_CanPlay = true;
				_UI_InGame._CG_遮罩.alpha = 0f;
				_UI_InGame._CG_遮罩.blocksRaycasts = false;
			}
		}
		MonoSingleton<MySceneManager>.Instance._event_触发切换场景 += delegate
		{
			_Can传送 = false;
			_CanPlay = false;
			F_隐藏Tips();
			PlayerPrefs.SetFloat("游戏时长", _GameTime);
			PlayerPrefs.Save();
		};
		MonoSingleton<MySceneManager>.Instance._event_成功切换场景 += delegate
		{
			_Can传送 = true;
			_CanPlay = true;
			F_隐藏Tips();
			_PC.transform.position = MonoSingleton<MySceneManager>.Instance.F_Get_MapPonit_传送();
			_UI_InGame._CG_遮罩.blocksRaycasts = false;
			_UI_InGame._CG_遮罩.DOFade(0f, 0.8f).SetEase(Ease.InQuint);
		};
		MonoSingleton<MySceneManager>.Instance._event_上香或者复活切换场景 += delegate
		{
			_Can传送 = true;
			_CanPlay = true;
			F_隐藏Tips();
			_PC.transform.position = _Pos_直面天命;
			_PC.gameObject.SetActive(value: true);
			_CanPlay = true;
			_UI_InGame._CG_遮罩.blocksRaycasts = false;
			_UI_InGame._CG_遮罩.DOFade(0f, 0.8f).SetEase(Ease.InQuint);
		};
	}

	public bool F_EnemyCanInit(string id)
	{
		if (_Dict_EnemyID_CanInit.TryGetValue(id, out var value))
		{
			return value;
		}
		_Dict_EnemyID_CanInit.Add(id, value: true);
		return true;
	}

	public void F_EnemyDie(string id)
	{
		if (_Dict_EnemyID_CanInit.ContainsKey(id))
		{
			_Dict_EnemyID_CanInit[id] = false;
		}
		else
		{
			_Dict_EnemyID_CanInit.Add(id, value: false);
		}
	}

	public void F_打坐(Vector2 pos)
	{
		Debug.LogError("F_打坐<<114514");
		if (MonoSingleton<MyDataManager>.Instance._PlayerData._第一次打坐)
		{
			MonoSingleton<MyDataManager>.Instance._PlayerData._第一次打坐 = false;
			MonoSingleton<MyDataManager>.Instance.F_保存数据();
			if (_PC._Curr等级 < _PC._mMax等级)
			{
				_PC.F_增加经验(_PC._mMax下一级所需经验);
			}
			else
			{
				_PC.F_初始化属性();
			}
		}
		_PC.F_打坐(pos);
	}

	public void F_上香(bool 上香 = true)
	{
		Debug.LogError($"_Can上香<<{_Can上香}");
		if (_Can上香)
		{
			if (上香)
			{
				MonoSingleton<MyDataManager>.Instance.F_上香(MonoSingleton<MySceneManager>.Instance.F_GetCurrSceneID());
			}
			_PC._Is死亡 = false;
			StartCoroutine(Cor_重生());
		}
	}

	private IEnumerator Cor_重生()
	{
		_CanPlay = false;
		_Can上香 = false;
		_UI_InGame._CG_遮罩.blocksRaycasts = true;
		_UI_InGame._CG_遮罩.DOFade(1f, 0.8f).SetEase(Ease.OutQuint);
		yield return new WaitForSeconds(0.8f);
		F_直面天命();
		yield return new WaitForSeconds(0.2f);
		_Can上香 = true;
	}

	public void F_重新开始()
	{
		MonoSingleton<MyDataManager>.Instance.F_重置数据();
		F_直面天命();
	}

	public void F_直面天命()
	{
		_传送动画结束 = true;
		_UI_InGame.F_显示();
		Time.timeScale = 1f;
		_Dict_EnemyID_CanInit.Clear();
		_暂停游戏 = false;
		_UI_InGame.F_UI_定身术CD(1f);
		_PC.F_设置定身术CD(1f);
		int 上香地图ID = MonoSingleton<MyDataManager>.Instance._PlayerData._上香地图ID;
		string mapName = "Map_黑风山_01_苍狼林_前山1";
		_Pos_直面天命 = MonoSingleton<MySceneManager>.Instance.F_Get_MapPonit_传送(1, 1);
		if (MonoSingleton<MyDataManager>.Instance._Data_Scene.F_MapPonit_上香_是否存在(上香地图ID))
		{
			mapName = MonoSingleton<MyDataManager>.Instance._Data_Scene._List_MapName[上香地图ID];
			_Pos_直面天命 = MonoSingleton<MySceneManager>.Instance.F_Get_MapPonit_上香(上香地图ID);
		}
		_PC.F_初始化属性();
		_PC.ChangeToState(StateType_Player.正常移动);
		MonoSingleton<MySceneManager>.Instance.F_直面天命(mapName);
	}

	public void F_敲钟(int 铜钟ID)
	{
		Debug.LogError("敲钟");
		MonoSingleton<MyDataManager>.Instance.F_敲钟(铜钟ID);
	}

	public void F_UI_定身术CD(float time)
	{
		_UI_InGame.F_UI_定身术CD(time);
	}

	public void F_注册本关卡玩家(PlayerController pc)
	{
		MonoSingleton<InputContrl>.Instance.F_IsOK();
		_PC = pc;
		_UI_InGame.F_注册玩家时绑定事件(pc);
	}

	public void F_显示得胜(string name)
	{
		_UI_InGame.F_显示得胜(name);
	}

	public void F_通过传送区域切换场景(string nextMapName, int next传送点ID)
	{
		if (!_Can传送)
		{
			return;
		}
		if (string.IsNullOrWhiteSpace(nextMapName) || next传送点ID == -1 || next传送点ID == 0)
		{
			Debug.LogError($"转送地图为、传送点id不真正确,nextMapName={nextMapName},next传送点I={next传送点ID}");
			return;
		}
		_UI_InGame._CG_遮罩.blocksRaycasts = true;
		_传送动画结束 = false;
		_UI_InGame._CG_遮罩.DOFade(1f, 0.8f).SetEase(Ease.OutQuint).OnComplete(delegate
		{
			_传送动画结束 = true;
		});
		MonoSingleton<MySceneManager>.Instance.F_通过传送区域切换场景(nextMapName, next传送点ID);
	}

	public void F_开始教程(Enum_教程类型 教程类型)
	{
		Debug.LogError($"GameManager<<开始教程<<{教程类型}");
		_UI_InGame.F_开始教程(教程类型);
	}

	private void Update()
	{
		if (!_CanPlay)
		{
			return;
		}
		if (_开启时间积累)
		{
			_GameTime += Time.deltaTime;
		}
		if (Input.GetKeyDown(KeyCode.Escape))
		{
			if (_暂停游戏)
			{
				F_继续游戏();
			}
			else
			{
				F_暂停游戏();
			}
		}
	}

	private void OnDestroy()
	{
		PlayerPrefs.SetFloat("游戏时长", _GameTime);
		PlayerPrefs.Save();
	}

	public void F_暂停游戏()
	{
		if (_CanPlay)
		{
			_UI_InGame.F_暂停游戏();
			_暂停游戏 = true;
			Time.timeScale = 0f;
			AudioListener.pause = true;
		}
	}

	public void F_继续游戏()
	{
		_UI_InGame.F_继续游戏();
		_暂停游戏 = false;
		Time.timeScale = 1f;
		AudioListener.pause = false;
	}

	public void F_解锁定身术()
	{
		_UI_InGame.F_显示定身术();
	}

	public void F_返回主页()
	{
		_UI_InGame.F_继续游戏();
		_UI_InGame.F_隐藏();
		MonoSingleton<MySceneManager>.Instance.F_返回主页();
		_PC.gameObject.SetActive(value: false);
		_CanPlay = false;
		_UI_InGame.F_隐藏();
		_UI_InGame._CG_遮罩.alpha = 1f;
		AudioListener.pause = false;
	}

	public void F_显示Tips(string str)
	{
		_UI_InGame.F_显示Tips(str);
	}

	public void F_隐藏Tips()
	{
		_UI_InGame.F_隐藏Tips();
	}

	public void F_获得经验(float val)
	{
		if (_PC != null)
		{
			_PC.F_增加经验(val);
		}
	}

	public void F_喝酒()
	{
		if (_PC != null)
		{
			_PC.F_喝酒();
		}
	}

	public void F_棍花Down()
	{
		if (_PC != null)
		{
			_PC.F_棍花Down();
		}
	}

	public void F_棍花Up()
	{
		if (_PC != null)
		{
			_PC.F_棍花Up();
		}
	}

	public void F_跳跃()
	{
		if (_PC != null)
		{
			_PC.F_跳跃();
		}
	}

	public void F_轻攻击()
	{
		_ = _PC != null;
		_PC.F_轻攻击();
	}

	public void F_重攻击Down()
	{
		if (_PC != null)
		{
			_PC.F_重攻击Down();
		}
	}

	public void F_重攻击Up()
	{
		if (_PC != null)
		{
			_PC.F_重攻击Up();
		}
	}

	public void F_翻滚()
	{
		if (_PC != null)
		{
			_PC.F_翻滚();
		}
	}

	public void F_定身术()
	{
		if (_PC != null)
		{
			_PC.F_定身术();
		}
	}

	public void F_快速升级()
	{
		if (_CanPlay)
		{
			F_获得经验(50f);
		}
	}
}
